package cn.com.thingo.shyintelligent.ui.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.media.MediaPlayer;
import android.text.TextUtils;

import com.th.opensdk.openapi.AlarmMessageInfo;
import com.th.opensdk.openapi.BaseControlInfo;
import com.th.opensdk.openapi.ConstantInfo;
import com.th.opensdk.openapi.NotificationInfo;
import com.th.opensdk.openapi.RoomInfo;
import com.th.opensdk.openapi.THOpenSDK;

import java.io.IOException;
import java.util.List;

import cn.com.thingo.shyintelligent.app.App;
import cn.com.thingo.shyintelligent.ui.activity.AddGatewayActivity;
import cn.com.thingo.shyintelligent.ui.activity.GatewayListActivity;
import cn.com.thingo.shyintelligent.ui.activity.MainActivity;
import cn.com.thingo.shyintelligent.ui.activity.SelectGatewayActivity;
import cn.com.thingo.shyintelligent.util.ToastUtil;
import me.yokeyword.fragmentation.SupportActivity;

public class THOpenReceiver extends BroadcastReceiver implements MediaPlayer.OnSeekCompleteListener {
    private SupportActivity activity;
    private CallBack callBack;
    private MediaPlayer player;
    private boolean isPlay = false;

    public THOpenReceiver(SupportActivity activity) {
        this.activity = activity;
        if (activity instanceof AddGatewayActivity || activity instanceof GatewayListActivity) {
            this.callBack = (CallBack) activity;
        }
    }

    public THOpenReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (TextUtils.equals(action, NotificationInfo.BROADCAST_GATEWAY_STATUS_CHANGED)) {
            //网关登录
            int status = intent.getIntExtra("status", -1);
            if (status == ConstantInfo.GATEWAY_STATUS_LOCAL_CONNECTION || status == ConstantInfo.GATEWAY_STATUS_REMOTE_CONNECTION) {
                if (activity instanceof AddGatewayActivity) {
                    doSome();
                } else if (activity instanceof SelectGatewayActivity) {
                    Intent mainIntent = new Intent(activity, MainActivity.class);
                    activity.startActivity(mainIntent);
                    ((SelectGatewayActivity) activity).hideProgress();
                    activity.finish();
                } else if (activity instanceof GatewayListActivity) {
                    callBack.callBack();
                }
            } else {
                if (activity instanceof SelectGatewayActivity) {
                    ((SelectGatewayActivity) activity).gatewayConnectFail();
                } else if (activity instanceof GatewayListActivity) {
                    if (App.getInstance().isSelect()) {
                        ((GatewayListActivity) activity).gatewayConnectFail();
                    }
                }
            }
            App.getInstance().setGatewayLoginStatus(true);
        } else if (TextUtils.equals(action, NotificationInfo.BROADCAST_DEVICE_STATE_UPLOAD)) {
            //场景模式开启
            long controlId = intent.getLongExtra("controlId", -1);
            if (controlId == App.getInstance().getCurrentScenesId()) {
                int controlType = intent.getIntExtra("controlType", -1);
                if (controlType == BaseControlInfo.CONTROL_TYPE_SCENE) {
                    App.getInstance().setScenesStatus(true);
                    ToastUtil.shortShow("部署完毕！");
                }
            }
        } else if (TextUtils.equals(action, NotificationInfo.BROADCAST_ALARM_MESSAGE_UPLOAD)) {
            if (activity instanceof MainActivity) {
                //报警消息
                AlarmMessageInfo alarmMessageInfo = (AlarmMessageInfo) intent.getSerializableExtra("alarmMessageInfo");
                ((MainActivity) activity).callBackMessage(alarmMessageInfo);
            }
            if (!isPlay) {
                playRing();
                isPlay = true;
            }
        }
    }

    @Override
    public void onSeekComplete(MediaPlayer mp) {
        isPlay = false;
        releasePlayer();
    }

    public interface CallBack {
        void callBack();
    }


    /**
     * 播放警报
     */
    private void playRing() {
        try {
            player = new MediaPlayer();
            AssetManager assetManager = activity.getAssets();
            AssetFileDescriptor fileDescriptor = assetManager.openFd("police.mp3");
            player.setDataSource(fileDescriptor.getFileDescriptor(), fileDescriptor.getStartOffset(),
                    fileDescriptor.getStartOffset());
            player.prepare();
            player.start();
            player.setOnSeekCompleteListener(this);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 释放player资源
     */
    private void releasePlayer() {
        player.stop();
        player.release();
        player = null;
    }


    public void doSome() {
        List<RoomInfo> roomList = THOpenSDK.getInstance().getRoomList();
        if (roomList.isEmpty()) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            doSome();
        } else {
            callBack.callBack();
        }
    }

}
